from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]


@app.get('/')
def hello():
    return {'status': "ok"}


@app.get('/item/{item_id}')
def item(item_id: int, q: Union[str, None]):
    return {'item_id': item_id, 'q': q}


@app.get('/ite/{name}')  # url/ite/sushi
def get_name(name: str):
    return {'name': name}


# http://127.0.0.1:8000/aop?skip=1&limit=1
# 在query 下面设置参数
@app.get('/aop')
async def aop(skip: int = 0, limit: int = 10):
    return fake_items_db[skip:skip + limit]


# 协程  async  await
@app.post('/info')
async def info():
    return {'name': 'sushi'}


class Item(BaseModel):
    name: str
    pwd: str
    age: int
    address: str | None = None


#请求参数
# {
#     "name":"sushi",
#     "pwd":"we233",
#     "age":23,
#     "address":"www"
# }
@app.post('/itemobj')
async def itemObj(item: Item):
    return item
